//=====================================================================================
// All Rights Reserved , Copyright © Learun 2014
//=====================================================================================

using DotNet.Kernel;
using DotNet.Utilities;
using BPMS.Entity;
using System.Collections;
using System.Text;
using BPMS.Service;
using System.Data;
using System;

namespace BPMS.Service
{
    /// <summary>
    /// 系统角色菜单列表表头定义关系
    /// <author>
    ///		<name>shecixiong</name>
    ///		<date>2014.01.07</date>
    /// </author>
    /// </summary>
    public class BPMS_RoleMenuTableColumnsDAL
    {
        /// <summary>
        /// 加载列表资源，列访问控制
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysMenu()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT 
                            SM.MenuId, 
                            SM.FullName, 
                            SM.Img
                            FROM BPMS_SysMenu SM where sm.MenuId in(SELECT MenuId FROM BPMS_TableColumns)");
            return DataFactory.SqlHelper().GetDataTableBySQL(strSql);
        }
        /// <summary>
        /// 加载列表字段资源，字段访问控制
        /// </summary>
        /// <param name="RoleId">角色主键</param>
        /// <returns></returns>
        public DataTable GetTableColumns(string RoleId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT 
                            tc.TableColumnsId,
                            tc.MenuId,
                            tc.Title ,
                            RMT.MenuId AS IsExist
                            FROM BPMS_TableColumns TC     
                            LEFT JOIN BPMS_RoleMenuTableColumns RMT on tc.MenuId=RMT.MenuId and TC.TableColumnsId=RMT.TableColumnsId
                            AND TC.IsPublic = 1 
                            and RMT.RoleId = @RoleId");
            SqlParam[] param = {
                                         new SqlParam("@RoleId",RoleId)};
            return DataFactory.SqlHelper().GetDataTableBySQL(strSql, param);
        }
        /// <summary>
        /// 分配角色字段权限
        /// </summary>
        /// <param name="KeyValue">主键值</param>
        /// <param name="RoleId">角色主键</param>
        /// <param name="CreateUserId">操作用户主键</param>
        /// <param name="CreateUserName">操作用户</param>
        /// <returns></returns>
        public int AddFieldPermission(string[] KeyValue, string RoleId, string CreateUserId, string CreateUserName)
        {
            StringBuilder[] sqls = new StringBuilder[KeyValue.Length + 1];
            object[] objs = new object[KeyValue.Length + 1];
            sqls[0] = DbCommon.DeleteSql("BPMS_RoleMenuTableColumns", "RoleId");
            objs[0] = new SqlParam[] { new SqlParam("@RoleId", RoleId) };
            int index = 1;
            foreach (string item in KeyValue)
            {
                if (item.Length > 0)
                {
                    BPMS_RoleMenuTableColumns entity = new BPMS_RoleMenuTableColumns();
                    string[] stritem = item.Split('|');
                    entity.RoleMenuTableColumnsId = CommonHelper.GetGuid;
                    entity.RoleId = RoleId;
                    entity.MenuId = stritem[0];
                    entity.TableColumnsId = stritem[1];
                    entity.SortCode = index;
                    entity.CreateDate = DateTime.Now;
                    entity.CreateUserId = CreateUserId;
                    entity.CreateUserName = CreateUserName;
                    sqls[index] = DbCommon.InsertSql(entity);
                    objs[index] = DbCommon.GetParameter(entity);
                    index++;
                }
            }
            return DataFactory.SqlHelper().BatchExecuteBySql(sqls, objs);
        }
    }
}